package gsw.tool.skin;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;

import java.lang.reflect.Method;
import java.util.Observable;

import gsw.tool.skin.utils.SkinPreference;
import gsw.tool.skin.utils.SkinResources;
import gsw.tool.skin.utils.Tool;

/**
 * 换肤管理器
 */
public class SkinManager extends Observable {
    private static SkinManager skinManager;
    private Application application;

    private SkinManager(Application application) {
        this.application = application;
        SkinPreference.init(application);
        SkinResources.init(application);
        //监听所有的Activity：获取View
        application.registerActivityLifecycleCallbacks(new SkinActivityLifeCallback());

        //加载保存的皮肤
        loadSkin(SkinPreference.getInstance().getSkin());
    }

    public static void init(Application application) {
        if (skinManager == null) {
            synchronized (SkinManager.class) {
                if (skinManager == null) {
                    skinManager = new SkinManager(application);
                }
            }
        }
    }

    /**
     * // TODO: 2018/3/24  待开发
     * <p>
     * 增加需要修改的属性
     *
     * @param application 初始化
     * @param attrs       额外需要修改的属性
     */
    public static void init(Application application, String[] attrs) {
        if (skinManager == null) {
            synchronized (SkinManager.class) {
                if (skinManager == null) {
                    skinManager = new SkinManager(application);
                }
            }
        }
    }

    public static SkinManager getInstance() {
        return skinManager;
    }

    /**
     * 加载皮肤包并更新view
     *
     * @param path 皮肤包路径
     */
    public void loadSkin(String path) {
        //还原皮肤
        if (Tool.isEmpty(path)) {
            SkinPreference.getInstance().setSkin("");
            SkinResources.getInstance().reset();
        } else {
            try {
                AssetManager assetManager = AssetManager.class.newInstance();
                //将皮肤包添加到资源管理器
                Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
                addAssetPath.setAccessible(true);
                addAssetPath.invoke(assetManager, path);

                Resources resources = application.getResources();
                //获取默认资源的横竖屏与语言参数
                Resources newResource = new Resources(assetManager, resources.getDisplayMetrics(), resources.getConfiguration());
                //获取皮肤包的包名
                PackageManager packageManager = application.getPackageManager();
                PackageInfo info = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
                String packageName = info.packageName;
                //加载皮肤包
                SkinResources.getInstance().applySkin(newResource, packageName);
                //保存当前使用的皮肤包
                SkinPreference.getInstance().setSkin(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //应用皮肤包
        setChanged();
        //通知观察者
        notifyObservers();
    }
}
